<?php

namespace Biz\model\FineGoodsBrokeNews;

use Doraemon\pockets\cache\Redis;
use Medoo\Medoo;

class FineGoodsBrokeNewsModel extends \Doraemon\pockets\datebase\BaseDbModel
{
    public const _CONFIG_FILE_ = 'db_store';

    public const _CONFIG_SELECT_ = 'db_brand_activity';

    public const _DATABASE_ = 'brand_activity';

    public const _TABLE_NAME = 'fine_goods_broke_news';

    /** @var int 数据状态：正常 */
    public const DATA_ACTIVE = 0;

    /** @var int 数据状态已删除 */
    public const DATA_DELETED = 1;

    /**
     * 保存好物爆料信息
     * @param $data
     * @return bool|\PDOStatement
     */
    public static function save($data)
    {
        return self::insert($data);
    }

    /**
     * 获取好物爆料最大批次
     * @param $uid
     * @return int
     */
    public static function getPid($uid): int
    {
        return (int)self::max('pid', ['uid' => $uid]) + 1;
    }

    /**
     * 获取好物爆料列表
     * @param $field
     * @param $map
     * @return array|bool
     */
    public static function list($field, $map)
    {
        $rs = self::select($field, $map);
        $wikiIDList = array_column($rs, 'basic_id');
        return array_values(array_unique($wikiIDList));
    }

    public static function getMaxPidPreUid($map)
    {
        $list =  self::select(['pid' => Medoo::raw('max(pid)'), 'uid'], $map);
        return array_column($list, 'pid', 'uid');
    }

    public static function getNotePreUid(array $uidArray)
    {
        $list = self::select(['uid','note'],['pid' => 1,'uid' => $uidArray]);
        $map = [];
        foreach ($list as $index => $item) {
            if($item['note'] == "null"){
                $note = [];
            }else{
                $note = json_decode($item['note'],true);
            }
            $map[$item['uid']] = $note;
        }
        return $map;
    }
}